﻿package mode7.ui
{
	
	import com.mode7.events.SelectionEvent;
	import com.mode7.interfaces.IInteractiveSelection;
	
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * @title 			SimpleButton
	 * @description 	"Simple button that extends a Sprite."
	 * @version 		3.0
	 * @since			6/25/2010
	 * @author			James Allen | Lunchbox www.lbox.com
	 * @author 			James Allen | Mode 7 Interactive www.mode7interactive.com
	 **/
	
	public class SimpleButton extends Sprite implements IInteractiveSelection
	{				
		private var _data:Object//catchall Object - can hold label, index, reverseIndex, anything you want, etc.
		private var _enabled:Boolean;//enables or disables the button, removing or adding handlers
		private var _selected:Boolean;//use this to "select" the button via code - will dispatch SELECTED event
		private var _index:int;
		
		public function SimpleButton()
		{
			internal_init();
		}
		
		public function mouseClick():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		
		public function mouseDoubleClick():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.DOUBLE_CLICK));
		}
		
		public function mouseOut():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.ROLL_OUT));
		}
		
		public function mouseOver():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.ROLL_OVER));
		}
		
		public function mouseUp():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
		}
		
		public function mouseDown():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
		}
		
		
		
		
		
		public function get enabled():Boolean { return _enabled; }
		
		public function set enabled(value:Boolean):void 
		{
			if (_enabled == value)
			{
				return;
			}
			toggleHandlers(value);
			_enabled = value;
		}
		
		public function get selected():Boolean { return _selected; }
		
		public function set selected(value:Boolean):void 
		{
			if (_selected == value || (!_enabled && value))
			{
				return;
			}
			if (value)
			{
				internal_select();
			}
			else
			{
				internal_unselect();
			}
			_selected = value;
		}
		
		public function get data():Object
		{
			return _data;
		}
		
		public function set data(value:Object):void
		{
			_data = value;
		}
		
		public function get io():InteractiveObject
		{
			return this;
		}
		
		private function internal_init():void
		{
			enabled = true;
			focusRect = false;
		}
		
		private function internal_select():void
		{
			if (!_selected)
			{
				_selected = true;
				select();
				dispatchEvent(new SelectionEvent(SelectionEvent.SELECT));
			}
		}
		
		protected function select():void
		{
			
		}
		
		private function internal_unselect():void
		{
			if (_selected)
			{
				_selected = false;
				unselect();
				dispatchEvent(new SelectionEvent(SelectionEvent.UNSELECT));
			}
			
		}
		
		protected function unselect():void
		{
			
		}
		
		
		
		
		private function toggleHandlers(value:Boolean):void
		{
			if (value == true)
			{
				buttonMode = true;
				mouseEnabled = true;
				mouseChildren = false;
				addEventListener(MouseEvent.CLICK, internal_click);
				addEventListener(MouseEvent.ROLL_OVER, internal_over);
				addEventListener(MouseEvent.ROLL_OUT, internal_out);
				addEventListener(MouseEvent.MOUSE_DOWN, internal_down);
				addEventListener(MouseEvent.MOUSE_UP, internal_up);
				addEventListener(MouseEvent.DOUBLE_CLICK, internal_doubleclick);
			}
			else
			{
				buttonMode = false;
				mouseEnabled = false;
				mouseChildren = false;
				removeEventListener(MouseEvent.CLICK, internal_click);
				removeEventListener(MouseEvent.ROLL_OVER, internal_over);
				removeEventListener(MouseEvent.ROLL_OUT, internal_out);
				removeEventListener(MouseEvent.MOUSE_DOWN, internal_down);
				removeEventListener(MouseEvent.MOUSE_UP, internal_up);
				removeEventListener(MouseEvent.DOUBLE_CLICK, internal_doubleclick);
			}
			
		}
		
		private function internal_out(e:MouseEvent):void
		{
			out(e);
		}
		
		protected function out(e:MouseEvent):void
		{
			
		}
		
		private function internal_over(e:MouseEvent):void
		{
			over(e);
		}
		
		protected function over(e:MouseEvent):void
		{
			
		}
		
		private function internal_up(e:MouseEvent):void 
		{
			up(e);
		}
		
		protected function up(e:MouseEvent):void
		{
			
		}
		
		private function internal_down(e:MouseEvent):void 
		{
			down(e);
		}
		
		protected function down(e:MouseEvent):void
		{
			
		}
		
		private function internal_doubleclick(e:MouseEvent):void 
		{
			doubleclick(e);
		}
		
		protected function doubleclick(e:MouseEvent):void
		{
			
		}
		
		private function internal_click(e:MouseEvent):void 
		{
			if (selected)
			{
				selected = false;
			}
			else
			{
				selected = true;
			}
			click(e);
		}
		
		protected function click(e:MouseEvent):void 
		{
			
		}
		
		public function get index():int
		{
			return _index;
		}

		public function set index(value:int):void
		{
			_index = value;
		}
		
		


	}
}